#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/6/17 15:50
# @Author  : Chenmm
# @File    : user.py
# @Software: PyCharm

from fastapi import APIRouter, Depends, Request

from methods.user_method import *
from middlewares.mysql.database import create_db


user_router = APIRouter()


@user_router.post("/register", summary="注册")
async def register(user: UserCreate, db: Session = Depends(create_db)):
    return db_create_user(db, user)


@user_router.post("/login", summary="登录")
async def login(request: Request, user: UserLogin, db: Session = Depends(create_db)):
    return await verify_login(request, user, db)



@user_router.get("/info", summary="获取个人信息")
async def get_user_info(user: UsernameRole = Depends(get_current_user), db: Session = Depends(create_db)):
    _user = get_user_by_username(db, user.username)
    data = {
        "username": _user.username,
        "sex": _user.sex,
        "age": _user.age
    }
    if user.role == "学生":
        data["student_num"] = _user.student_num
    else:
        data["job_Num"] = _user.job_num
    logger.info(f"用户：{_user.username} 获取用户信息成功")
    return response(data=data)


@user_router.post("/password", summary="修改密码")
async def change_password(
    request: Request,
    param_password: UserChangePassword,
    _user: UsernameRole = Depends(get_current_user),
    db: Session = Depends(create_db)
):
    return await change_password_method(request, param_password, _user, db)